package com.sample.springbatch.job.sample.config;

import com.sample.springbatch.job.config.BaseConfig;
import com.sample.springbatch.job.sample.processor.RepayOrderProcessor;
import com.sample.springbatch.job.sample.reader.RepayOrderJdbcPagingItemReader;
import com.sample.springbatch.job.sample.reader.RepayOrderParamsFlatFileItemReader;
import com.sample.springbatch.job.sample.writer.RepayOrderParamsFlatFileItemWriter;
import com.sample.springbatch.model.RepayOrderInfo;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;


/**
 * 作业获取参数示例
 * <b>在配置中通过@@Value("#{jobParameters[times]}")获取参数，需要添加@StepScope注解</b>
 * <b>在类内部通过@Value("#{jobParameters[times]}")获取参数，需要添加@JobScope注解</b>
 * <b>分区作业配置中的分区参数通过@Value("#{stepExecutionContext[userId]}")在类里面获取，
 *      需要添加@StepScope注解，示例参见：PartitionerStepConfig</b>
 * @author zwg
 * @date 2017/7/14.
 */
@Configuration
public class JobParamsConfig extends BaseConfig {


    /**
     * 获取job参数示例  参数通过注解获取
     * @return
     */
    @Bean(name="repayOrderJdbcPagingItemReaderOfParams")
    @JobScope
    public RepayOrderJdbcPagingItemReader<RepayOrderInfo> repayOrderJdbcPagingItemReaderOfParams(){
        RepayOrderJdbcPagingItemReader<RepayOrderInfo> reader = new RepayOrderJdbcPagingItemReader<>();
        return reader;
    }

    /**
     * 获取job参数示例
     * @param times  直接获取参数值
     * @param params 获取全部参数
     * @return
     */
    @Bean(name="repayOrderFlatFileItemReaderOfParams")
    @StepScope
    public RepayOrderParamsFlatFileItemReader<RepayOrderInfo> repayOrderFlatFileItemReaderOfParams(@Value("#{jobParameters[times]}") String times,@Value("#{jobParameters}") Map params){
        System.out.println("repayOrderFlatFileItemReaderOfParams================times:"+times);
        System.out.println("repayOrderFlatFileItemReaderOfParams================times:"+params.keySet().toString());
        RepayOrderParamsFlatFileItemReader<RepayOrderInfo> reader = new RepayOrderParamsFlatFileItemReader<>();
        if(params!=null && params.containsKey("filePath")){
            reader.setFilePath(params.get("filePath").toString());
        }

        return reader;
    }

    /**
     * 获取job参数示例
     * @param times  直接获取参数值
     * @param params 获取全部参数
     * @return
     */
    @Bean(name="repayOrderFlatFileItemWriterOfParams")
    @StepScope
    public RepayOrderParamsFlatFileItemWriter<RepayOrderInfo> repayOrderFlatFileItemWriterOfParams(@Value("#{jobParameters[times]}") String times,@Value("#{jobParameters}") Map params){
        System.out.println("repayOrderFlatFileItemWriterOfParams================times:"+times);
        System.out.println("repayOrderFlatFileItemWriterOfParams================times:"+params.keySet().toString());
        RepayOrderParamsFlatFileItemWriter<RepayOrderInfo> writer = new RepayOrderParamsFlatFileItemWriter<>();
        if(params!=null && params.containsKey("writerFilePath")){
            writer.setFilePath(params.get("writerFilePath").toString());
        }

        return writer;
    }

    @Bean
    public RepayOrderProcessor repayOrderProcessor() {
        return new RepayOrderProcessor();
    }



    /**
     * 获取作业参数的JOB
     * @param repayOrderStepOfParams
     * @return
     */
    @Bean(name = "repayOrderJobOfParams")
    public Job repayOrderJobOfParams(Step repayOrderStepOfParams) {
        return jobBuilderFactory.get("repayOrderJobOfParams")
                .incrementer(new RunIdIncrementer())
                //.listener(listener)
                .flow(repayOrderStepOfParams)
                .end()
                .build();
    }

    /**
     * 获取作业参数的step
     * @param repayOrderFlatFileItemReaderOfParams
     * @return
     */
    @Bean(name="repayOrderStepOfParams")
    public Step repayOrderStepOfParams(RepayOrderParamsFlatFileItemReader<RepayOrderInfo> repayOrderFlatFileItemReaderOfParams,
                                       RepayOrderParamsFlatFileItemWriter<RepayOrderInfo> repayOrderFlatFileItemWriterOfParam) {
        return stepBuilderFactory.get("repayOrderStepOfParams")
                .<RepayOrderInfo, RepayOrderInfo> chunk(100)
                .reader(repayOrderFlatFileItemReaderOfParams)
                .processor(repayOrderProcessor())
                .writer(repayOrderFlatFileItemWriterOfParam)
                //.taskExecutor(new SimpleAsyncTaskExecutor())
                //.throttleLimit(5)
                .build();
    }


    @Bean(name = "repayOrderJobOfParams2")
    public Job repayOrderJobOfParams2() {
        return jobBuilderFactory.get("repayOrderJobOfParams2")
                .incrementer(new RunIdIncrementer())
                //.listener(listener)
                .flow(repayOrderStepOfParams2())
                .end()
                .build();
    }

    /**
     * 获取作业参数的step
     * @return
     */
    @Bean(name="repayOrderStepOfParams2")
    public Step repayOrderStepOfParams2() {
        return stepBuilderFactory.get("repayOrderStepOfParams2")
                .<RepayOrderInfo, RepayOrderInfo> chunk(1)
                .reader(repayOrderJdbcPagingItemReaderOfParams())
                .processor(repayOrderProcessor())
                .build();
    }
}
